﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class EquipmentListImportForm : System.Windows.Forms.Form
	{
		public EquipmentListImportForm()
		{
			this.InitializeComponent();
			this.m_Attributes = new Dictionary<EquipmentField, string>();
		}

		private void EquipmentListImportForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.cmbExcel.Visible = false;
			this.dataGridView.Controls.Add(this.cmbExcel);
			this.ReadFormDataFromXml();
			this.UpdateDataGridViewData(true);
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("ImportEquipmentFromExcel");
			string text = Path.Combine(Product.InstallLocation, "UserWork");
			string attributeEx = xmlHelper.GetAttributeEx("ExcelPath", text);
			this.txtExcelFile.Text = (string.IsNullOrWhiteSpace(attributeEx) ? text : attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("StartRow", "2");
			this.txtStartRow.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("Number", "A");
			this.m_Attributes.Add(EquipmentField.Field_Number, attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("Name", "B");
			this.m_Attributes.Add(EquipmentField.Field_Name, attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("Level", "C");
			this.m_Attributes.Add(EquipmentField.Field_Level, attributeEx);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("ImportEquipmentFromExcel");
			xmlHelper.SetAttribute("ExcelPath", this.txtExcelFile.Text.Trim());
			xmlHelper.SetAttribute("StartRow", this.txtStartRow.Text.Trim());
			xmlHelper.SetAttribute("Number", this.m_Attributes[EquipmentField.Field_Number]);
			xmlHelper.SetAttribute("Name", this.m_Attributes[EquipmentField.Field_Name]);
			xmlHelper.SetAttribute("Level", this.m_Attributes[EquipmentField.Field_Level]);
			xmlHelper.SaveXml("");
		}

		private string TitleOfField(EquipmentField field)
		{
			switch (field)
			{
			case EquipmentField.Field_Number:
				return "设备编号";
			case EquipmentField.Field_Name:
				return "设备名称";
			case EquipmentField.Field_Level:
				return "标高";
			default:
				return string.Empty;
			}
		}

		private void UpdateDataGridViewData(bool flag)
		{
			if (flag)
			{
				this.dataGridView.Rows.Clear();
				using (Dictionary<EquipmentField, string>.Enumerator enumerator = this.m_Attributes.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						KeyValuePair<EquipmentField, string> keyValuePair = enumerator.Current;
						DataGridViewRow dataGridViewRow = new DataGridViewRow();
						int index = this.dataGridView.Rows.Add(dataGridViewRow);
						dataGridViewRow = this.dataGridView.Rows[index];
						dataGridViewRow.Cells[0].Value = this.TitleOfField(keyValuePair.Key);
						dataGridViewRow.Cells[1].Value = keyValuePair.Value;
						dataGridViewRow.Tag = keyValuePair;
					}
					return;
				}
			}
			for (int i = 0; i < this.dataGridView.Rows.Count; i++)
			{
				DataGridViewRow dataGridViewRow2 = this.dataGridView.Rows[i];
				KeyValuePair<EquipmentField, string> keyValuePair2 = (KeyValuePair<EquipmentField, string>)dataGridViewRow2.Tag;
				this.m_Attributes[keyValuePair2.Key] = dataGridViewRow2.Cells[1].Value.ToString();
			}
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
		{
			try
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
				openFileDialog.InitialDirectory = Path.GetDirectoryName(this.txtExcelFile.Text.Trim());
				if (openFileDialog.ShowDialog(this) == DialogResult.OK)
				{
					this.txtExcelFile.Text = openFileDialog.FileName;
				}
			}
			catch (Exception)
			{
				MessageBox.Show("获取Excel文件路径出错！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}

		private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count)
			{
				return;
			}
			if (e.ColumnIndex == 0)
			{
				this.cmbExcel.Visible = false;
				return;
			}
			this.m_rowIndex = e.RowIndex;
			this.m_columnIndex = e.ColumnIndex;
			Rectangle cellDisplayRectangle = this.dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
			this.cmbExcel.Left = cellDisplayRectangle.Left;
			this.cmbExcel.Top = cellDisplayRectangle.Top;
			this.cmbExcel.Width = cellDisplayRectangle.Width;
			this.cmbExcel.Height = cellDisplayRectangle.Height;
			this.cmbExcel.Text = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
			this.cmbExcel.Visible = true;
		}

		private void cmbExcel_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.dataGridView.Rows[this.m_rowIndex].Cells[this.m_columnIndex].Value = this.cmbExcel.Text;
		}

		private void buttonImport_Click(object sender, EventArgs e)
		{
			this.m_ExcelFile = this.txtExcelFile.Text.Trim();
			this.m_StartRow = Convert.ToInt32(this.txtStartRow.Text.Trim());
			this.UpdateDataGridViewData(false);
			if (!File.Exists(this.m_ExcelFile))
			{
				MessageBox.Show("Excel文件不存在！", "盈建科提示", MessageBoxButtons.OK);
				return;
			}
			if ((double)this.m_StartRow < this.txtStartRow.MostValue.Minimal)
			{
				MessageBox.Show("起始行值设置过小，请重新设置！", "盈建科提示", MessageBoxButtons.OK);
				this.txtStartRow.Focus();
				return;
			}
			this.SaveFormDataToXml();
			base.Close();
			base.DialogResult = DialogResult.OK;
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private const string XmlNode_Root = "ImportEquipmentFromExcel";

		private const string XmlNode_ExcelPath = "ExcelPath";

		private const string XmlNode_StartRow = "StartRow";

		private const string XmlNode_Number = "Number";

		private const string XmlNode_Name = "Name";

		private const string XmlNode_Level = "Level";

		private const string Item_Number = "设备编号";

		private const string Item_Name = "设备名称";

		private const string Item_Level = "标高";

		private int m_rowIndex;

		private int m_columnIndex = 1;

		public Dictionary<EquipmentField, string> m_Attributes;

		public string m_ExcelFile = string.Empty;

		public int m_StartRow = 2;
	}
}
